package com.amt.olcacon.controller.result;

import com.amt.olcacon.db.DerbyConfig;
import com.amt.olcacon.vo.GlobalResponse;
import lombok.extern.slf4j.Slf4j;
import org.openlca.core.DataDir;
import org.openlca.core.database.ResultDao;
import org.openlca.core.model.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Ethan.Yang
 * @CreateTime: 2025-08-14
 */
@Slf4j
@RestController
@RequestMapping("result")
public class ResultController {

    @GetMapping("/getAll")
    public GlobalResponse<List<Result>> getAll() {
        try (var db = DataDir.get().openDatabase(DerbyConfig.DBNAME)) {
            ResultDao dao = new ResultDao(db);

            List<Result> results = dao.getAll();
            results.forEach(result -> {
                result.flowResults.clear();
                result.impactResults.clear();
                result.impactMethod = null;
                result.productSystem = null;
                result.referenceFlow = null;
                result.category = null;
            });

            return GlobalResponse.of(results);
        } catch (Exception ex) {
            return null;
        }
    }

}
